package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.utils.Constants;

public class Militia extends Action {

	public Militia() {
		this.cost = 4;
	}

	@Override
	public Card clone() {
		return new Militia();
	}

	@Override
	public String toString() {
		return Constants.MILITIA;
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("+$2\n");
		sb.append("Each other player discards down to 3 cards in hand.\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		player.changeAction(-1);
		player.changeMoney(2);
		playedCard(player);

		for (Player current : players)
		{
			if (current.equals(player))
			{
				continue;
			}
			Constants.printer.print("Player: " + current.getName());
			if (current.hasCounter())
			{
				Constants.printer.print(current.getName() + " has a Moat and is unaffected.");
			}
			else
			{
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				String choice = null;
				while (current.getHand().size() > 3)
				{
					Constants.printer.print("Must discard down to 3 cards. Type a card to discard");
					Constants.printer.print(current.printHand());
					choice = in.readLine().trim().toLowerCase();
					if (current.getHand().contains(choice))
					{
						Card card = current.getHand().drawCard(choice);
						current.addToDeck(card, Constants.DISCARD_DECK);
						Constants.printer.print(card.toString() + " has been discarded");
					}
					else
					{
						Constants.printer.print("Card not in hand.");
					}
				}
			}
		}
	}
}
